<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\User;
use app\admin\model\Marketing as MarketingModer;
use app\admin\model\Configuration;
use app\admin\model\Announcement;
use think\Db;
use think\Exception;

/**
 * 案例接口
 */
class Marketing extends Api
{
    protected $NeedLogin = []; //存在则验证token

	public function _initialize(){
		$action = request()->action();
	
		if(in_array($action,$this->NeedLogin)){
			$ignore = array('host','accept','content-length','content-type');
			$headers = array();
			if($this->request->request('token')){
				$headers['token'] = $this->request->request('token');
			}else{
				foreach($_SERVER as $key=>$value){
					if(substr($key, 0, 5)==='HTTP_'){
						$key = substr($key, 5);
						$key = str_replace('_', ' ', $key);
						$key = str_replace(' ', '-', $key);
						$key = strtolower($key);
						if(!in_array($key, $ignore)){
							$headers[$key] = $value;
						}
					}
				}
			}
			$token=$headers['token'];
			if(strlen($token)!=32){
				$this->error('请登录');
			}
			$this->user=User::where(array('token'=>$token))->find();
			if(!$this->user){
				$this->error('请登录');
			}
			if($this->user['status'] != 1){
                $this->error('账号异常');
            }
			User::where(array('id'=>$this->user['id']))->update(array('active_time'=>time()));
		}

		$this->configuration = Configuration::find();
	}


    //获取案例列表
    public function getMarketingList(){
        $list = MarketingModer::where(['status'=>1])->orderRaw('rand()')->limit(5)->select();
        $this->success('操作成功',$list);
    }

    //获取案例列表
    public function getAnnouncement(){
        $list = Announcement::orderRaw('rand()')->limit(100)->select();
        $this->success('操作成功',$list);
    }

	//获取详情
    public function getInfo(){
		$id = $this->request->request('id','');
		if(!$id){
			$this->error('参数错误');
		}
        $info = MarketingModer::where(['id'=>$id])->find();
		if(!$info){
			$this->error('参数错误');
		}
		if($info['bg_day']){
			$info['bg_day'] = str_replace('，', ',', $info['bg_day']);
			
			$bg_day = explode(',',$info['bg_day']);
		
			foreach ($bg_day as &$v) {
			    $v = $v*1;
			}
			$info['bg_day'] = $bg_day;
		}
		if($info['bg_data']){
			$info['bg_data'] = str_replace('，', ',', $info['bg_data']);
			$bg_data = explode(',',$info['bg_data']);
		
			foreach ($bg_data as &$v) {
			    $v = $v*1;
			}
			$info['bg_data'] = $bg_data;
		}
		
		$info['img_lunbo'] = explode(',',$info['img_lunbo']);
		
        $this->success('操作成功',$info);
    }


}